//
//  DxyApiRequest.m
//  DxyClient
//
//  Created by Xu Jiwei on 11-5-17.
//  Copyright 2011 Dxy.cn. All rights reserved.
//

#import "WCApiRequest.h"
#import "WCUserContext.h"

@implementation WCApiRequest

+ (ASIHTTPRequest *)postRequestWithApiURL:(NSString *)apiUrl needOAuth:(BOOL)needOAuth {
    return [[self alloc] initWithApiURL:apiUrl needOAuth:needOAuth];
}

+ (ASIHTTPRequest *)getRequestWithApiURL:(NSString *)apiUrl needOAuth:(BOOL)needOAuth{
    ASIHTTPRequest *request = [[self alloc] initWithApiURL:apiUrl needOAuth:needOAuth];
    [request setRequestMethod:@"GET"];
    return request;
}
+ (ASIHTTPRequest *)putRequestWithApiURL:(NSString *)apiUrl needOAuth:(BOOL)needOAuth{
    ASIHTTPRequest *request = [[self alloc] initWithApiURL:apiUrl needOAuth:needOAuth];
    [request setRequestMethod:@"PUT"];
    return request;
}
+ (ASIHTTPRequest *)deleteRequestWithApiURL:(NSString *)apiUrl needOAuth:(BOOL)needOAuth{
    ASIHTTPRequest *request = [[self alloc] initWithApiURL:apiUrl needOAuth:needOAuth];
    [request setRequestMethod:@"DELETE"];
    return request;
}

- (id)initWithApiURL:(NSString *)apiUrl needOAuth:(BOOL)needOAuth{
    NSLog(@"request url:%@",apiUrl);
    if (self = [super initWithURL:[NSURL URLWithString:[apiUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]) {
        if (needOAuth) {
            [self addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"OAuth2 %@", [WCUserContext sharedInstance].accessToken]];
        }
    }
    return self;
}

@end
